package com.example.androidtest.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.androidtest.R;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class MyClientDemo extends Activity {

    private Button send = null;

    private TextView info = null;
     Socket client;
    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        this.send = findViewById(R.id.button);

        this.info = findViewById(R.id.tv);

        this.send.setOnClickListener(new SendOnClickListener());

    }
    BufferedReader buf;
    private class SendOnClickListener implements View.OnClickListener {

        @Override

        public void onClick(View v) {

            try {

// 读取返回的数据

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        try {
                            client = new Socket("192.168.1.4", 8888);

                            buf = new BufferedReader(new InputStreamReader(

                                    client.getInputStream()));

                            ObjectOutputStream oos = new ObjectOutputStream(

                                    client.getOutputStream());

                            UploadFile myFile = SendOnClickListener.this.getUploadFile();

                            oos.writeObject(myFile);//写文件对象

                            oos.close();

                        } catch (Exception e) {
                            try {
                                client.close();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }

                    }

                }).start();

//                String result = buf.readLine(); // 接收返回信息
//
//                System.out.println("**************** " + result);
//
//                if ("true".equals(result)) {
//
//                    MyClientDemo.this.info.setText("操作成功！");
//
//                } else {
//
//                    MyClientDemo.this.info.setText("操作失败！");
//
//                }
//
//                buf.close();



            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        private UploadFile getUploadFile() throws Exception { // 包装了传送数据

            UploadFile myFile = new UploadFile();

            myFile.setTitle("DISNEY公园"); // 设置标题

            myFile.setMimeType("image/jpeg"); // 图片的类型

            File file = new File(getFilesDir()

                    .toString() + File.separator + "kotlincn-docs.pdf");

            InputStream input = null;

            try {

                input = new FileInputStream(file); // 从文件中读取

                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                byte data[] = new byte[1024];

                int len = 0;

                while ((len = input.read(data)) != -1) {

                    bos.write(data, 0, len);

                }

                myFile.setContentData(bos.toByteArray());

                myFile.setContentLength(file.length());

                myFile.setExt("jpeg");

            } catch (Exception e) {

                throw e;

            } finally {

                input.close();

            }

            return myFile;

        }

    }

}
